﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using GestionPedago.EntityContext;
using GestionPedago.Persistence.OPManager;

namespace Persistence.OPManager
{
    /// <summary>
    /// Manager de persistance de Promotion
    /// </summary>
    public abstract class PromotionOPManager
    {
        /// <summary>
        /// Méthode de récupération d'une liste de promotion triée par libelle ou date de début
        /// </summary>
        /// <param name="orderByLibelleAsc">Tri par libellé Asc ou desc</param>
        /// <param name="orderByDebutAsc">Tri par date de début Asc ou desc</param>
        /// <returns>La liste des promotions</returns>
        public static List<Promotion> GetListePromotions(bool? orderByLibelleAsc = true, bool? orderByDebutAsc = null)
        {
            try
            {
                IQueryable<Promotion> query = EntityConnection.GetContext().Promotion;
                if (orderByLibelleAsc != null)
                {
                    if ((bool)orderByLibelleAsc)
                        query = query.OrderBy(it => it.Libelle);
                    else
                        query = query.OrderByDescending(it => it.Libelle);
                }
                else if (orderByDebutAsc != null)
                {
                    if ((bool)orderByDebutAsc)
                        query = query.OrderBy(it => it.Debut);
                    else
                        query = query.OrderByDescending(it => it.Debut);
                }

                /* query.Select(it => it);*/

                List<Promotion> lst = query.ToList();

                return lst;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Méthode de récupération d'une promotion par son code
        /// </summary>
        /// <param name="code">Identifiant de la promotion</param>
        /// <returns>La promotion</returns>
        public static Promotion GetPromotionByCode(string code)
        {

            try
            {
                var query = from it in EntityConnection.GetContext().Promotion
                            where it.CodePromotion == code
                            select it;

                Promotion promotion = null;

                promotion = query.First();

                return promotion;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Méthode de récupération d'une liste de code promotion pour suggestion de planningECFDetail
        /// </summary>
        /// <param name="codePlanning">PlanningIndividuelFormation concerné</param>
        /// <param name="idECF">ECF concerné</param>
        /// <returns>La liste des codes promotions</returns>
        public static List<String> GetListePromotionsSuggestionPlanification(int codePlanning, int idECF) {
            try {
                List<String> lst = new List<String>();
                PlanningIndividuelFormation pif = PlanningIndividuelFormationOPManager.GetPlanningIndividuelFormationByCode(codePlanning);
               
                IQueryable<PlanningECF> queryPe = EntityConnection.GetContext().PlanningECF;
                queryPe = EntityConnection.GetContext().PlanningECF;
                queryPe = queryPe.Where(it => (it.ECF.IdECF == idECF && it.DateECF >= pif.DateInscription));
                queryPe = queryPe.OrderBy(it => it.DateECF);
               // pe = queryPe.FirstOrDefault();
                //List<String> lst = queryPe.ToList();
                foreach(PlanningECF pe in queryPe.ToList()) {
                    lst.Add(pe.CodePromotion);
                }
                
               return lst;
            } catch(Exception ex) {
                throw ex;
            }
        }

        /// <summary>
        /// Méthode de récupération d'une liste de code promotion pour suggestion de planningECFDetail
        /// </summary>
        /// <param name="codePlanning">PlanningIndividuelFormation concerné</param>
        /// <param name="idECF">ECF concerné</param>
        /// <returns>La liste des codes promotions</returns>
        public static bool VerificationCoherenceDateSuggeree(DateTime dateSuggeree, string codePromotion, int idECF) {
            try {
                bool res;
               
                IQueryable<PlanningECF> queryPe = EntityConnection.GetContext().PlanningECF;
                queryPe = EntityConnection.GetContext().PlanningECF;
                queryPe = queryPe.Where(it => (it.ECF.IdECF == idECF && it.DateECF == dateSuggeree && it.CodePromotion == codePromotion));

                if(queryPe.Count() > 0) {
                    res = true;
                } else {
                    res = false;
                }

                return res;
            } catch(Exception ex) {
                throw ex;
            }
        }
    }
}
